package com.fjnldx.mes.service.environment;

import com.fjnldx.mes.domain.domainReq.PageRequest;
import com.fjnldx.mes.domain.domainReq.TempHumidityPageReq;
import com.fjnldx.mes.domain.domainReq.TempHumidityReq;
import com.fjnldx.mes.domain.domainRes.Result;
import com.fjnldx.mes.domain.domainRes.TempAndHumidityInfoRes;
import com.fjnldx.mes.domain.energy.vo.TempHumidityPageVO;
import com.fjnldx.mes.domain.pojo.WenshiduTable;
import com.fjnldx.mes.mapper.environment.EnvironmentMapper;
import com.fjnldx.mes.utils.Common;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.record.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EnvironmentService {

    @Autowired
    private Common common;
    @Autowired
    private EnvironmentMapper environmentMapper;

    /**
     * 获取今日整点的细纱机温湿度
     *
     * @param machineNumber
     * @return
     */
    public Result getTempHumidityByIntegralPoint(String machineNumber) {
        List<TempAndHumidityInfoRes> resultList = new ArrayList<>();
        String todayStr = common.getTodayStr();
        List<WenshiduTable> wenshiduTableList = environmentMapper.getTempHumidityByIntegralPoint(todayStr);
        wenshiduTableList.forEach(tempHumidity -> {
            TempAndHumidityInfoRes tempHumidityInfo = new TempAndHumidityInfoRes();
            tempHumidityInfo.setTime(tempHumidity.getTimeStr());
            tempHumidityInfo.setMachineNumber(machineNumber);
            switch (machineNumber) {
                case "xs1":
                    tempHumidityInfo.setTemp(tempHumidity.getXswd1());
                    tempHumidityInfo.setHumidity(tempHumidity.getXssd1());
                    break;
                case "xs2":
                    tempHumidityInfo.setTemp(tempHumidity.getXswd2());
                    tempHumidityInfo.setHumidity(tempHumidity.getXssd2());
                    break;
                case "xs3":
                    tempHumidityInfo.setTemp(tempHumidity.getXswd3());
                    tempHumidityInfo.setHumidity(tempHumidity.getXssd3());
                    break;
                case "xs4":
                    tempHumidityInfo.setTemp(tempHumidity.getXswd4());
                    tempHumidityInfo.setHumidity(tempHumidity.getXssd4());
                    break;
                case "xs5":
                    tempHumidityInfo.setTemp(tempHumidity.getXswd5());
                    tempHumidityInfo.setHumidity(tempHumidity.getXssd5());
                    break;
                case "xs6":
                    tempHumidityInfo.setTemp(tempHumidity.getXswd6());
                    tempHumidityInfo.setHumidity(tempHumidity.getXssd6());
                    break;
                case "bn7":
                    tempHumidityInfo.setTemp(tempHumidity.getBnwd7());
                    tempHumidityInfo.setHumidity(tempHumidity.getBnsd7());
                    break;
                case "lt8":
                    tempHumidityInfo.setTemp(tempHumidity.getLtwd8());
                    tempHumidityInfo.setHumidity(tempHumidity.getLtsd8());
                    break;
                case "lt9":
                    tempHumidityInfo.setTemp(tempHumidity.getLtwd9());
                    tempHumidityInfo.setHumidity(tempHumidity.getLtsd9());
                    break;
            }
            resultList.add(tempHumidityInfo);
    });
    Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("id",machineNumber);
        resultMap.put("data",resultList);
        return Result.success(resultMap);
}

    /**
     * 获取细纱机最近的温湿度
     *
     * @return
     */
    public Result getTempHumidityByCurrentTime() {
        List<TempAndHumidityInfoRes> resultList = new ArrayList<>();
        WenshiduTable tempAndHumidityInfo = environmentMapper.getTempHumidityByCurrentTime();
        TempAndHumidityInfoRes tempHumidityInfo1 = new TempAndHumidityInfoRes();
        TempAndHumidityInfoRes tempHumidityInfo2 = new TempAndHumidityInfoRes();
        TempAndHumidityInfoRes tempHumidityInfo3 = new TempAndHumidityInfoRes();
        TempAndHumidityInfoRes tempHumidityInfo4 = new TempAndHumidityInfoRes();
        TempAndHumidityInfoRes tempHumidityInfo5 = new TempAndHumidityInfoRes();
        TempAndHumidityInfoRes tempHumidityInfo6 = new TempAndHumidityInfoRes();
        TempAndHumidityInfoRes tempHumidityInfo7 = new TempAndHumidityInfoRes();
        TempAndHumidityInfoRes tempHumidityInfo8 = new TempAndHumidityInfoRes();
        TempAndHumidityInfoRes tempHumidityInfo9 = new TempAndHumidityInfoRes();
        tempHumidityInfo5.setMachineNumber("xs1");
        tempHumidityInfo6.setMachineNumber("xs2");
        tempHumidityInfo7.setMachineNumber("xs3");
        tempHumidityInfo1.setMachineNumber("xs4");
        tempHumidityInfo2.setMachineNumber("xs5");
        tempHumidityInfo3.setMachineNumber("xs6");
        tempHumidityInfo4.setMachineNumber("bn7");
        tempHumidityInfo8.setMachineNumber("lt8");
        tempHumidityInfo9.setMachineNumber("lt9");
        tempHumidityInfo5.setMachineName("细纱温度1");
        tempHumidityInfo6.setMachineName("细纱温度2");
        tempHumidityInfo7.setMachineName("细纱温度3");
        tempHumidityInfo1.setMachineName("细纱温度4");
        tempHumidityInfo2.setMachineName("细纱温度5");
        tempHumidityInfo3.setMachineName("细纱温度6");
        tempHumidityInfo4.setMachineName("细纱温度7");
        tempHumidityInfo8.setMachineName("络筒温度8");
        tempHumidityInfo9.setMachineName("络筒温度9");
        tempHumidityInfo1.setTemp(tempAndHumidityInfo.getXswd4());
        tempHumidityInfo2.setTemp(tempAndHumidityInfo.getXswd5());
        tempHumidityInfo3.setTemp(tempAndHumidityInfo.getXswd6());
        tempHumidityInfo4.setTemp(tempAndHumidityInfo.getBnwd7());
        tempHumidityInfo5.setTemp(tempAndHumidityInfo.getXswd1());
        tempHumidityInfo6.setTemp(tempAndHumidityInfo.getXswd2());
        tempHumidityInfo7.setTemp(tempAndHumidityInfo.getXswd3());
        tempHumidityInfo8.setTemp(tempAndHumidityInfo.getLtwd8());
        tempHumidityInfo9.setTemp(tempAndHumidityInfo.getLtwd9());
        tempHumidityInfo1.setHumidity(tempAndHumidityInfo.getXssd4());
        tempHumidityInfo2.setHumidity(tempAndHumidityInfo.getXssd5());
        tempHumidityInfo3.setHumidity(tempAndHumidityInfo.getXssd6());
        tempHumidityInfo4.setHumidity(tempAndHumidityInfo.getBnsd7());
        tempHumidityInfo5.setHumidity(tempAndHumidityInfo.getXssd1());
        tempHumidityInfo6.setHumidity(tempAndHumidityInfo.getXssd2());
        tempHumidityInfo7.setHumidity(tempAndHumidityInfo.getXssd3());
        tempHumidityInfo8.setHumidity(tempAndHumidityInfo.getLtsd8());
        tempHumidityInfo9.setHumidity(tempAndHumidityInfo.getLtsd9());
        resultList.add(tempHumidityInfo5);
        resultList.add(tempHumidityInfo6);
        resultList.add(tempHumidityInfo7);
        resultList.add(tempHumidityInfo1);
        resultList.add(tempHumidityInfo2);
        resultList.add(tempHumidityInfo3);
        resultList.add(tempHumidityInfo4);
        resultList.add(tempHumidityInfo8);
        resultList.add(tempHumidityInfo9);
        return Result.success(resultList);
    }

    /**
     * 获取细纱机历史的温湿度表格数据
     *
     * @param tempHumidityPageReq
     * @param pageRequest
     * @return
     */
    public Result getHistoryTempHumidityByPage(TempHumidityPageReq tempHumidityPageReq, PageRequest pageRequest) {

        Map<String, String> queryProperty = new HashMap<>();
        List<String> devSn = tempHumidityPageReq.getCt();
        // 根据参数中的ct号, 设置查询的字段值
        for (int i = 0; i < devSn.size(); i++) {
            switch (devSn.get(i)) {
                case "1":
                    queryProperty.put("温湿度表1", "min(xswd1) minTemp, max(xswd1) maxTemp, min(xssd1) minHumidity, max(xssd1) maxHumidity");
                    break;
                case "2":
                    queryProperty.put("温湿度表2", "min(xswd2) minTemp, max(xswd2) maxTemp, min(xssd2) minHumidity, max(xssd2) maxHumidity");
                    break;
                case "3":
                    queryProperty.put("温湿度表3", "min(xswd3) minTemp, max(xswd3) maxTemp, min(xssd3) minHumidity, max(xssd3) maxHumidity");
                    break;
                case "4":
                    queryProperty.put("温湿度表4", "min(xswd4) minTemp, max(xswd4) maxTemp, min(xssd4) minHumidity, max(xssd4) maxHumidity");
                    break;
                case "5":
                    queryProperty.put("温湿度表5", "min(xswd5) minTemp, max(xswd5) maxTemp, min(xssd5) minHumidity, max(xssd5) maxHumidity");
                    break;
                case "6":
                    queryProperty.put("温湿度表6", "min(xswd6) minTemp, max(xswd6) maxTemp, min(xssd6) minHumidity, max(xssd6) maxHumidity");
                    break;
                case "7":
                    queryProperty.put("温湿度表7", "min(bnwd7) minTemp, max(bnwd7) maxTemp, min(bnsd7) minHumidity, max(bnsd7) maxHumidity");
                    break;
                case "8":
                    queryProperty.put("温湿度表8", "min(ltwd8) minTemp, max(ltwd8) maxTemp, min(ltsd8) minHumidity, max(ltsd8) maxHumidity");
                    break;
                case "9":
                    queryProperty.put("温湿度表9", "min(ltwd9) minTemp, max(ltwd9) maxTemp, min(ltsd9) minHumidity, max(ltsd9) maxHumidity");
                    break;
            }
        }
        tempHumidityPageReq.setCt(devSn);
        tempHumidityPageReq.setQueryProperty(queryProperty);
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<TempHumidityPageVO> resultList = environmentMapper.getHistoryTempHumidityByPage(tempHumidityPageReq);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        // 设置结束时间
        resultList.forEach(result -> {
            String startTime = result.getStartTime();
            LocalDateTime startLocalDateTime = LocalDateTime.parse(startTime, dateTimeFormatter);
            LocalDateTime endLocalDateTime = startLocalDateTime.plusMinutes(30);
            String endTime = dateTimeFormatter.format(endLocalDateTime);
            result.setEndTime(endTime);
        });
        return Result.success(new PageInfo<>(resultList));
    }

    /**
     * 获取历史温湿度数据的折线图数据
     *
     * @param tempHumidityReq
     * @return
     */
    public Result getHistoryTempHumidity(TempHumidityReq tempHumidityReq) {
        String devSn = tempHumidityReq.getDevSn();
        Map<String, String> params = new HashMap<>();
        switch (devSn) {
            case "1":
                params.put("xswd1", "temp");
                params.put("xssd1", "humidity");
                break;
            case "2":
                params.put("xswd2", "temp");
                params.put("xssd2", "humidity");
                break;
            case "3":
                params.put("xswd3", "temp");
                params.put("xssd3", "humidity");
                break;
            case "4":
                params.put("xswd4", "temp");
                params.put("xssd4", "humidity");
                break;
            case "5":
                params.put("xswd5", "temp");
                params.put("xssd5", "humidity");
                break;
            case "6":
                params.put("xswd6", "temp");
                params.put("xssd6", "humidity");
                break;
            case "7":
                params.put("bnwd7", "temp");
                params.put("bnsd7", "humidity");
                break;
            case "8":
                params.put("ltwd8", "temp");
                params.put("ltsd8", "humidity");
                break;
            case "9":
                params.put("ltwd9", "temp");
                params.put("ltsd9", "humidity");
                break;
        }
        tempHumidityReq.setQueryProperty(params);
        List<TempAndHumidityInfoRes> result = environmentMapper.getHistoryTempHumidity(tempHumidityReq);
        return Result.success(result);
    }

    public Result getTempHumidityTable(List<Integer> dictCodes) {
        List<Map<String, String>> table = environmentMapper.getTempHumidityTable(dictCodes);
        return Result.success(table);
    }
}
